from fastapi import APIRouter
from fastapi.responses import FileResponse

from backend.const import CDN_IMAGE_DIR
from common.path import ROOT_DIR
from common.response import ServerErrorResponse


'''
本文件的路由定义规则：一些通用基础类的路由定义。
例如：
1、图片服务接口定义
'''
router = APIRouter()


@router.get("/image")
def analysis_image(url: str):
    '''本接口模拟图片服务器url，根据返回图片内容
    
    @url格式: 只需要cdn路径下的相对路径, 例如: analysis/gwas/image_name.png 或 image/xx.jpeg
    '''
    try:        
        image_dir = ROOT_DIR / CDN_IMAGE_DIR / url  
        print(type(image_dir))
        print(image_dir)
        print(image_dir.as_uri())
        print("image_dir is exists: ", image_dir.exists())
        return FileResponse(path=image_dir, media_type='image/png, image/jpeg, image/gif')
    except Exception as e:
        return ServerErrorResponse(data=repr(e))